﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Com.Rivyer.DAL
{
	/// <summary>
	/// 数据访问类:jobdoneplanDal
	/// </summary>
	public partial class jobdoneplanDal
	{
		public jobdoneplanDal()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "tb_jobdoneplan"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_jobdoneplan");
			strSql.Append(" where id="+id+" ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Com.Rivyer.Model.jobdoneplanModel model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.createdate != null)
			{
				strSql1.Append("createdate,");
				strSql2.Append("'"+model.createdate+"',");
			}
			if (model.realname != null)
			{
				strSql1.Append("realname,");
				strSql2.Append("'"+model.realname+"',");
			}
			if (model.daydone != null)
			{
				strSql1.Append("daydone,");
				strSql2.Append("'"+model.daydone+"',");
			}
			if (model.dayplan != null)
			{
				strSql1.Append("dayplan,");
				strSql2.Append("'"+model.dayplan+"',");
			}
			if (model.dayissue != null)
			{
				strSql1.Append("dayissue,");
				strSql2.Append("'"+model.dayissue+"',");
			}
			strSql.Append("insert into tb_jobdoneplan(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			strSql.Append(";select @@IDENTITY");
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Com.Rivyer.Model.jobdoneplanModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_jobdoneplan set ");
			if (model.createdate != null)
			{
				strSql.Append("createdate='"+model.createdate+"',");
			}
			if (model.realname != null)
			{
				strSql.Append("realname='"+model.realname+"',");
			}
			if (model.daydone != null)
			{
				strSql.Append("daydone='"+model.daydone+"',");
			}
			if (model.dayplan != null)
			{
				strSql.Append("dayplan='"+model.dayplan+"',");
			}
			if (model.dayissue != null)
			{
				strSql.Append("dayissue='"+model.dayissue+"',");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where id="+ model.id+"");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_jobdoneplan ");
			strSql.Append(" where id="+id+"" );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_jobdoneplan ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Com.Rivyer.Model.jobdoneplanModel GetModel(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" id,createdate,realname,daydone,dayplan,dayissue ");
			strSql.Append(" from tb_jobdoneplan ");
			strSql.Append(" where id="+id+"" );
			Com.Rivyer.Model.jobdoneplanModel model=new Com.Rivyer.Model.jobdoneplanModel();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["createdate"]!=null && ds.Tables[0].Rows[0]["createdate"].ToString()!="")
				{
					model.createdate=ds.Tables[0].Rows[0]["createdate"].ToString();
				}
				if(ds.Tables[0].Rows[0]["realname"]!=null && ds.Tables[0].Rows[0]["realname"].ToString()!="")
				{
					model.realname=ds.Tables[0].Rows[0]["realname"].ToString();
				}
				if(ds.Tables[0].Rows[0]["daydone"]!=null && ds.Tables[0].Rows[0]["daydone"].ToString()!="")
				{
					model.daydone=ds.Tables[0].Rows[0]["daydone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["dayplan"]!=null && ds.Tables[0].Rows[0]["dayplan"].ToString()!="")
				{
					model.dayplan=ds.Tables[0].Rows[0]["dayplan"].ToString();
				}
				if(ds.Tables[0].Rows[0]["dayissue"]!=null && ds.Tables[0].Rows[0]["dayissue"].ToString()!="")
				{
					model.dayissue=ds.Tables[0].Rows[0]["dayissue"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,createdate,realname,daydone,dayplan,dayissue ");
			strSql.Append(" FROM tb_jobdoneplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,createdate,realname,daydone,dayplan,dayissue ");
			strSql.Append(" FROM tb_jobdoneplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_jobdoneplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from tb_jobdoneplan T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
	}
}

